package com.ajie.ft_kvdata;

import android.app.Application;
import android.content.Context;
import android.content.ContextWrapper;
import android.util.Log;

import com.ajie.lib_base.ft_file.FileIOUtils;
import com.google.gson.Gson;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by zhangyingjie on 2020/10/17
 *
 * @function
 */
public class KvDataApplication extends Application {
    private static final String TAG = "KvDataApplication";
    private static KvDataApplication instance;

    public static KvDataApplication getInstance(){
        long start = System.currentTimeMillis();
        while (instance == null){
                try {
                    long current = System.currentTimeMillis();
                    if (current - start > 2000){
                        Log.e(TAG, "getInstance: null !!!" );
                        return instance;
                    }
                    Thread.sleep(10);
                    Log.i(TAG, "getInstance: wait!!! instance = null");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        return instance;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        instance = this;
        Log.i(TAG, "attachBaseContext: ");
//        changeSPLocation();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "onCreate: ");

    }

    private final String SP_DIR = "/bbox/media";
    private final String SP_FILE_PATH = SP_DIR +File.separator +"spCustom.txt";

    /**
     * TODO 结论修改失败
     * origin:null
     * origin:/bbox/media
     * newDir:/bbox/media
     * ///data/data/com.ajie.ft_kvdata/shared_prefs/kvdata.xml
     */
    public void changeSPLocation() {
        Log.i(TAG, "changeSPLocation: ");
        try {
            Field field = null;
            // 获取ContextWrapper对象中的mBase变量。该变量保存了ContextImpl对象
            field = ContextWrapper.class.getDeclaredField("mBase");
            field.setAccessible(true);
            // 获取mBase变量
            Object obj = field.get(this);
            // 获取ContextImpl。mPreferencesDir变量，该变量保存了数据文件的保存路径
            field = obj.getClass().getDeclaredField("mPreferencesDir");
            field.setAccessible(true);
            File originDir = (File) field.get(obj);
            if (originDir != null) {
                Log.i(TAG, "changeSPLocation: get mPreferencesDir origin:"+originDir.getAbsolutePath());
            }else {
                Log.e(TAG, "changeSPLocation: get mPreferencesDir origin null !!!" );
            }
            // 创建自定义路径
            File file = new File(SP_DIR);
             //修改mPreferencesDir变量的值
            field.set(obj, file);

//            FileIOUtils.writeFileFromString(SP_FILE_PATH,"test save from third app"); //java.io.IOException: Permission denied

            Class<?> clazz = obj.getClass();
            Log.i(TAG, "changeSPLocation: clazz: "+clazz);
            Log.i(TAG, "changeSPLocation: clazz: "+clazz.getName());
            field = clazz.getDeclaredField("mPreferencesDir");
            field.setAccessible(true);
            File newDir = (File) field.get(obj);
            Log.i(TAG, "changeSPLocation: get mPreferencesDir newDir:"+newDir.getAbsolutePath());

            Method getPreferencesDir = clazz.getDeclaredMethod("getPreferencesDir", null);
            Log.i(TAG, "changeSPLocation: ");
            getPreferencesDir.setAccessible(true);
//            File invoke = (File) getPreferencesDir.invoke(clazz,null);
            Log.i(TAG, "changeSPLocation: getPreferencesDir method info:"+getPreferencesDir.getName());
            File invoke = (File) getPreferencesDir.invoke(obj,null);
            if (invoke != null) {
                Log.i(TAG, "changeSPLocation: getPreferencesDir method :"+invoke.getAbsolutePath());
            }else {
                Log.i(TAG, "changeSPLocation: getPreferencesDir method : null !!!");
            }
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
